package com.metering.purchaseServerAdmin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import com.metering.purchaseServerAdmin.dao.MenuDao;
import com.metering.purchaseServerAdmin.dao.RoleMenuDao;
import com.metering.purchaseServerAdmin.domain.MenuDO;
import com.metering.purchaseServerAdmin.domain.Tree;
import com.metering.purchaseServerAdmin.dto.RouterDTO;
import com.metering.purchaseServerAdmin.service.MenuService;
import com.metering.purchaseServerAdmin.utils.BuildTree;

/**
 * 树型菜单
 * @ClassName: MenuServiceImpl
 * @Description: 
 * @Author GA
 * @DateTime 2018年12月20日 下午1:29:52
 */
public class MenuServiceImpl implements MenuService {
	@Autowired
	private MenuDao menuMapper;
    
	@Autowired
    private RoleMenuDao roleMenuMapper;
	

	@Cacheable //负责将方法的返回值加入到缓存中，参数3
	@Override
	public Tree<MenuDO> getSysMenuTree(Long id) {
		// TODO Auto-generated method stub
		List<Tree<MenuDO>> trees = new ArrayList<Tree<MenuDO>>();
        List<MenuDO> menuDOs = userMenus(id);
        for (MenuDO menuDO : menuDOs) {
            Tree<MenuDO> tree = new Tree<MenuDO>();
            tree.setId(menuDO.getMenuId().toString());
            tree.setParentId(menuDO.getParentId().toString());
            tree.setText(menuDO.getName());
            Map<String, Object> attributes = new HashMap<>(16);
            attributes.put("url", menuDO.getUrl());
            attributes.put("icon", menuDO.getIcon());
            tree.setAttributes(attributes);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<MenuDO> t = BuildTree.build(trees);
        return t;
	}

	/**
	 * value：缓存位置名称，不能为空，如果使用EHCache，就是ehcache.xml中声明的cache的name
　　　 *　key：缓存的key，默认为空，既表示使用方法的参数类型及参数值作为key，支持SpEL
	 */
	@Cacheable(value = "permission", key = "#userId")
	@Override
	public List<MenuDO> userMenus(Long userId) {
		// TODO Auto-generated method stub
		return menuMapper.listMenuByUserId(userId);
	}

	@CacheEvict(value = "permission", key = "#userId")//负责清除缓存
	@Override
	public List<Tree<MenuDO>> listMenuTree(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Tree<MenuDO> getTree() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Tree<MenuDO> getTree(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean clearCache(Long userId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<MenuDO> list(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int remove(Long id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int save(MenuDO menu) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(MenuDO menu) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public MenuDO get(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Set<String> listPerms(Long userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Long> MenuIdsByRoleId(Long roleId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> PermsByUserId(Long userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<RouterDTO> RouterDTOsByUserId(Long userId) {
		// TODO Auto-generated method stub
		return null;
	}

}
